package com.housediyadmin.view.components
{
	import com.housediyadmin.events.houseEvents.DeleteHouseEvent;
	import com.housediyadmin.events.houseEvents.EditHouseEvent;
	import com.housediyadmin.events.houseEvents.GetHousesEvent;
	import com.housediyadmin.model.BuildingModel;
	import com.housediyadmin.model.HouseModel;
	import com.housediyadmin.vo.House;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.DateField;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CloseEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class RentererHouseMediator extends Mediator
	{
		[Inject]
		public var view:RentererHouseView;
		
		[Inject]
		public var houseModel:HouseModel;
		
		[Inject]
		public var buildingModel:BuildingModel;
		
		public function RentererHouseMediator() 
		{
			super();
		}
		
		override public function onRegister():void
		{
			view.modifyBtn.addEventListener(MouseEvent.CLICK, modifyEve);		
			view.delBtn.addEventListener(MouseEvent.CLICK,delMouseEve);
			view.houseHoldCB.addEventListener(MouseEvent.CLICK,checkBoxSelected);
		}
		
		private function checkBoxSelected(e:MouseEvent):void
		{
			if(e.target.selected)
			{
				houseModel.selectedHouseArr.push(houseModel.selectedHouse);
				houseModel.buildingId = buildingModel.selectedBuilding.buildingID;
				houseModel.selectedHouse.isSelected=true;
			}				
			else
			{
				var index:int=houseModel.selectedHouseArr.indexOf(houseModel.selectedHouse);
				houseModel.selectedHouseArr.splice(index,1);
				houseModel.selectedHouse.isSelected=false;
			}
		}
		
		private function delMouseEve(e:MouseEvent):void
		{
			Alert.yesLabel="是";
			Alert.noLabel="否";
			Alert.cancelLabel="取消";
			Alert.show("删除相应的户型信息吗？","",3,view,deleteAlert);
		}
		
		private function deleteAlert(e:CloseEvent):void
		{
			if(e.detail == Alert.YES)
			{
				var houseIds:Array=new Array();
				houseIds.push(houseModel.selectedHouse.houseID);
				dispatch(new DeleteHouseEvent(DeleteHouseEvent.DELETE_HOUSES,houseIds, houseModel.selectedHouse.buildingID));
			}
		}
		
		private function modifyEve(event:MouseEvent):void
		{
			var popup:HousePopUp=new HousePopUp();
			popup.type="EDIT";
			PopUpManager.addPopUp(popup,contextView,true);
			PopUpManager.centerPopUp(popup);
			mediatorMap.createMediator(popup);
		}
	}
}